package com.app.controller;


import com.app.model.CustomerModel;
import com.app.model.JobAdditionalModel;
import com.app.model.UserModel;
import com.app.service.CustomerService;
import com.app.service.JobAdditionalService;
import com.app.service.MsgVerifyService;
import com.app.service.UserService;
import com.app.util.Result;
import com.app.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Controller
@RestController
public class JobAdditionalController {

    @Autowired
    private JobAdditionalService jobAdditionalService;
    @Autowired
    UserService userService;
    @Autowired
    private CustomerService customerService;



    @PostMapping("insertAdditional")
    public Result<Object> followAdd(
                            @RequestParam("other") String data,
                            HttpServletRequest request){
        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405,"token不存在");

        }
        String mobile = TokenUtils.verify(token);
        UserModel user = userService.getUserByMobile(mobile);

        if(user==null)
        {
            return Result.error(405,"用户不存在");

        }
        CustomerModel cus = customerService.getCustomerById(user.getCId());
        JobAdditionalModel model = new JobAdditionalModel();
        model.setCusId(cus.getId());
        model.setData(data);
        jobAdditionalService.delete(cus.getId());
        jobAdditionalService.insert(model);
       return Result.success(null);
    }

    @PostMapping("getAdditionalByCus")
    public Result<Object> getAdditionalByCus(HttpServletRequest request){
        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405,"token不存在");

        }
        String mobile = TokenUtils.verify(token);
        UserModel user = userService.getUserByMobile(mobile);

        if(user==null)
        {
            return Result.error(405,"用户不存在");

        }
        CustomerModel cus = customerService.getCustomerById(user.getCId());
        JobAdditionalModel jobAdditionalModel = jobAdditionalService.getModelByCid(cus.getId());
       return Result.success(jobAdditionalModel);
    }

    @GetMapping("/getAdditionalById")
    public Result<Object> getAdditionalById(HttpServletRequest request){
        String id = request.getParameter("id");
        String token = request.getHeader("token");

        CustomerModel cus;
        JobAdditionalModel model = null;
        if(id!=null&&!"0".equals(id)&&!"undefined".equals(id))
        {
            model = jobAdditionalService.getModelByCid(Integer.valueOf(id));
        }else {
            if(token==null)
            {
                return Result.error(405,"token不存在");
            }
            String mobile = TokenUtils.verify(token);
            UserModel user = userService.getUserByMobile(mobile);

            if(user==null)
            {
                return Result.error(405,"用户不存在");

            }
            cus = customerService.getCustomerById(user.getCId());
            JobAdditionalModel jobAdditionalModel = jobAdditionalService.getModelByCid(Integer.valueOf(cus.getId()));
            if(jobAdditionalModel!=null)
            {
                model = jobAdditionalModel;
            }
        }
       return Result.success(model);
    }

}
